import re

import scrapy

from kifa.items import KifaItem


class FlowerTradingSpider(scrapy.Spider):
    name = 'flower_trading'
    # allowed_domains = ['kifa.net.cn']
    start_urls = ['http://www.kifa.net.cn/info.do?method=pageQuery&type=%E8%A1%8C%E6%83%85%E5%88%86%E6%9E%90']


    def start_requests(self):

    def parse(self, response):
        page_total = response.xpath('//*[@id="queryform"]/ul/li[1]/text()').extract_first()
        page_total = int(re.findall(r'\d+', page_total)[0])
        print(page_total)
        for i in range(1, page_total):
            print(i)
            # print(page_total)
            url = 'http://www.kifa.net.cn/info.do?method=pageQuery&type=%E8%A1%8C%E6%83%85%E5%88%86%E6%9E%90'
            data = {'curPage': str(i)}
            yield scrapy.FormRequest(url=url, formdata=data, callback=self.parse_list)


    def parse_list(self, response, **kwargs):
        a_list = response.xpath('/html/body/div[3]/div[2]/div/div[2]/div/a')
        for a in a_list:
            # item = {}
            item = KifaItem()
            item['url'] = 'http://www.kifa.net.cn/' + a.xpath('./@href').extract_first()
            item['title'] = a.xpath('./text()').extract_first()
            yield scrapy.Request(url=item['url'], callback=self.parse_detail, meta={'item': item})


    def parse_detail(self, response, **kwargs):
        item = response.meta['item']
        item['time'] = response.xpath('//div[@class="subtitleshow"]/small/text()').extract_first()
        content_list = response.xpath('/html/body/div[3]/div[2]/div[1]//text()').extract()
        item['content'] = ''.join(content_list)
        yield item
        # print(item)
        # self.create_item(item)


    #     # 寻找范围
    #     for content in content_list:
    #         if re.findall(r'^2\.', content) or re.findall(r'非洲菊交易行情', content):
    #             meigui_index = content_list.index(content)
    #         else:
    #             meigui_index = 0
    #
    #
    #     content_list = content_list[:meigui_index]
    #     for content in content_list:
    #         trading_volume_total = re.findall(r'拍卖交易总量约为(\d+)|拍卖交易总量约(\d+)|拍市总交易量为(\d+)', content)
    #         if trading_volume_total:
    #             item['trading_volume_total'] = [i for i in trading_volume_total[0]if i != ''][0]
    #         trading_volume_curl = re.findall(r'花拍中心（KIFA）交易量为(\d+)', content)
    #         if trading_volume_curl:
    #             item['trading_volume_curl'] = trading_volume_curl[0]
    #
    #         data = item
    #         if re.findall(r'单头玫瑰', content):
    #             data['max_type'] = '单头玫瑰'
    #             try:
    #                 data['supply_position'] = re.findall(r'供货量较前一交易日(\S+?)\，', content)[0]
    #             except:
    #                 pass
    #             try:
    #                 data['market'] = re.findall(r'供货量较前一交易日\S+，交易行情(\S+)\，', content)[0]
    #             except:
    #                 pass
    #             try:
    #                 data['closing'] = re.findall(r'供货量较前一交易日\S+，交易行情\S+，成交率(\S+)。', content)[0]
    #             except:
    #                 pass
    #             self.parse_level(data, content_list)
    #         elif re.findall(r'多头玫瑰', content):
    #             data['max_type'] = '多头玫瑰'
    #
    #             try:
    #                 data['market'] = re.findall(r'.*\交易行情(\S+?)，', content)[0]
    #             except:
    #                 pass
    #             try:
    #                 data['closing'] = re.findall(r'.*\成交率(\S+?)，', content)[0]
    #             except:
    #                 pass
    #             try:
    #                 data['max_price'] = re.findall(r'最高价(.*?)\，', content)[0]
    #             except:
    #                 pass
    #             try:
    #                 data['ave_unitl_price'] = re.findall(r'各等级均价(.*?)。', content)[0]
    #             except:
    #                 pass
    #             self.create_item(data)
    #
    #
    #
    # def parse_level(self, data, content_list):
    #     for content in content_list:
    #         try:
    #             data['max_price'] = re.findall(r'最高价(.*?)\，', content)[0]
    #         except:
    #             pass
    #         try:
    #             data['ups_donws'] = re.findall(r'均价较昨日，(.)', content)[0]
    #         except:
    #             pass
    #         try:
    #             data['ups_donws_num'] = re.findall(r'均价较昨日，.(\S+)元', content)[0]
    #         except:
    #             pass
    #         if 'A级' in content:
    #             data['min_type'] = 'A级'
    #             self.create_item(data)
    #         if 'B级' in content:
    #             data['min_type'] = 'B级'
    #             self.create_item(data)
    #         if 'C级' in content:
    #             data['min_type'] = 'C级'
    #             self.create_item(data)

    def create_item(self, data):
        item = KifaItem()
        for key, val in data.items():
            item[key] = val
        yield item
